﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace SimpleTestFrameForNFC
{
    [InstrumentInterfaceTypeAnno(InstrumentInterfaceType.GPIB)]
    class ITECH8912E : BaseInstrument
    {
        private FunctionType type;

        string MEASURE_POWER_CMD = "FETCh:POWer?";
        string MEASURE_CURRENT_CMD = "MEASure:CURRent?";
        string MEASURE_VOLTAGE_CMD = "MEASure:VOLTage?";
        string ON_CMD = "INPUT ON";
        string OFF_CMD = "INPUT OFF";

        public double[] Measure()
        {
            double[] i = new double[3];
           i[0] = MeasureVoltage();
           i[1] = MeasureCurrent();
           i[2] = MeasurePower();
            return i;
        }

        public double MeasurePower()
        {

            string response = Query(MEASURE_POWER_CMD);
            return Convert.ToDouble(response);
        }

        public double MeasureCurrent()
        {
            string response = Query(MEASURE_CURRENT_CMD);
            return Convert.ToDouble(response);
        }

        public double MeasureVoltage()
        {
            string response = Query(MEASURE_VOLTAGE_CMD);
            return Convert.ToDouble(response);
        }

        public void On()
        {
            Write(ON_CMD);
        }

        public void Off()
        {
            Write(OFF_CMD);
        }

        public void SetFunction(FunctionType type)
        {
            string FUNCTION_CMD = "FUNC ";
            switch (type)
            {
                case FunctionType.VOLT:
                    FUNCTION_CMD = FUNCTION_CMD + "VOLT";
                    break;
                case FunctionType.CURR:
                    FUNCTION_CMD = FUNCTION_CMD + "CURR";
                    break;
                case FunctionType.RES:
                    FUNCTION_CMD = FUNCTION_CMD + "RES";
                    break;
            }
            this.type = type;
            Write(FUNCTION_CMD);
        }

        public void SenseOn(bool onOff)
        {
            string SENSE_CMD = onOff ? "REM:SENS 1" : "REM:SENS 0";
            Write(SENSE_CMD);
        }

        public void SetValue(double value)
        {
            string SET_VALUE_CMD = string.Empty;
            switch (type)
            {
                case FunctionType.VOLT:
                    SET_VALUE_CMD = "VOLT ";
                    break;
                case FunctionType.CURR:
                    SET_VALUE_CMD = "CURR ";
                    break;
                case FunctionType.RES:
                    SET_VALUE_CMD = "RES ";
                    break;
            }
            SET_VALUE_CMD += value;
            Write(SET_VALUE_CMD);
        }

        public void SetValueAndOn(double value)
        {
            SetValue(value);
            On();
        }

        /// <summary>
        /// 连续的设置负载，每设置一次负载，再设置等待时间
        /// </summary>
        /// <param name="values"></param>
        /// <param name="delayInEveryTimes"></param>
        public void SetMultipleValueAndOn(double[] values, int[] delayInEveryTimes)
        {
            if (delayInEveryTimes.Length < values.Length)
            {
                int[] newArr = new int[values.Length];
                Array.Copy(delayInEveryTimes, newArr, delayInEveryTimes.Length);
                delayInEveryTimes = newArr;
            }

            for (int i = 0; i < values.Length; i++)
            {
                SetValue(values[i]);
                if (i == 0)
                {
                    On();
                }
                Thread.Sleep(delayInEveryTimes[i]);
            }
        }

        /// <summary>
        /// 新增，用于设置多台设备
        /// </summary>
        /// <param name="ITECH8912Es"></param>
        /// <param name="value"></param>
        public static void ITECH8912EsSetValueAndOn(Dictionary<string, ITECH8912E> ITECH8912Es, double value)
        {
            foreach (ITECH8912E ITECH8912E in ITECH8912Es.Values)
            {
                ITECH8912E.SetValueAndOn(value);
            }
        }
    }
}
